package com.ruoyi.util.paypassword;

import com.ruoyi.system.domain.PayInfo;
import com.ruoyi.system.domain.TransferRequest;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.JoinPoint;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Aspect
@Component
public class PasswordValidationAspect {

    @Resource
    private PasswordService passwordService;
    /**
     * 支付密码验证逻辑
     * @param joinPoint 获取到的参数
     * @param payPassword 注解
     * 注解中包含发送者ID，用于验证支付密码是否正确
     * */
    @Before("@annotation(payPassword)")
    public void validatePassword(JoinPoint joinPoint, PayPassword payPassword) {
        Object[] args = joinPoint.getArgs();
        if (args.length > 0 && args[0] instanceof TransferRequest) {
            TransferRequest transferRequest = (TransferRequest) args[0];
            String password = transferRequest.getPayPassword();
            Long senderId = transferRequest.getSenderId();
            // 从注解中获取发送者ID
            // 调用PasswordService进行密码验证
            boolean isValid = passwordService.payPassword(password, senderId);
            if (!isValid) {
                throw new RuntimeException("支付密码错误");
            }
        } else {
            throw new RuntimeException("请求中缺少支付密码");
        }
    }
    /**
     * 修改密码验证逻辑，增加对支付密码过于简单的校验
     * @param joinPoint 获取到的参数
     * @param passwordVerifiers 注解
     * */
    @Before("@annotation(passwordVerifiers)")
    public void validatePassword(JoinPoint joinPoint,PasswordVerifiers passwordVerifiers) {
        Object[] args = joinPoint.getArgs();
        if (args.length > 0 && args[0] instanceof PayInfo) {
            PayInfo payInfo = (PayInfo) args[0];
            String password = payInfo.getPayPassword();
            // 从注解中获取发送者ID
            // 调用PasswordService进行密码验证
            boolean isValid = passwordService.passwordVerifiers(password);
            if (!isValid) {
                throw new RuntimeException("支付密码过于简单");
            }
        } else {
            throw new RuntimeException("请求中缺少支付密码");
        }
    }
}